<script setup lang="ts">
import { cancelOrder } from "~/api/order";
import { ElMessage } from "element-plus";

const props = defineProps({
  orderDetails: {
    type: Object,
    default: () => {},
  },
});
const emits = defineEmits(["close"]);
const checkList = ref<string[]>([]);

const close = () => {
  emits("close");
};
const cancelReason = ref("");
const otherReason = ref("");
const loading = ref(false);
const cancelOrderHandle = async () => {
  if (!checkList.value.length && !otherReason.value) {
    return ElMessage.warning("请选择或输入取消订单原因");
  }
  if (otherReason.value) {
    cancelReason.value = otherReason.value;
  } else {
    cancelReason.value = checkList.value.join(",");
  }
  loading.value = true;
  try {
    await cancelOrder({
      orderSn: props.orderDetails.orderSn,
      cancelReason: cancelReason.value,
    });
    emits("close", 1);
    ElMessage.success("取消订单成功");
  } catch (error) {
  } finally {
    loading.value = false;
  }
};
const otherReasonChange = () => {
  checkList.value = ["其他"];
  if (otherReason.value.length > otherReasonMaxLength) {
    otherReason.value = otherReason.value.slice(0, otherReasonMaxLength);
  }
};
// 其他原因输入框字数限制
const otherReasonMaxLength = 200;
const otherReasonLength = computed(() => {
  return otherReason.value.length;
});
</script>

<template>
  <div class="dialog">
    <div class="dialog-box">
      <div class="header">
        <div class="left">
          <img src="~/assets/dialog-icon.png" alt="" />
          <span>选择取消订单原因</span>
        </div>
        <img class="close" src="~/assets/modal-close.png" alt="" @click="close" />
      </div>
      <div class="dialog-content">
        <el-checkbox-group v-model="checkList">
          <p>
            <el-checkbox label="找到更合适的服务" value="找到更合适的服务" />
            <el-checkbox label="虛假信息" value="虛假信息" />
          </p>
          <p>
            <el-checkbox label="与卖家协商一致退款" value="与卖家协商一致退款" />
            <el-checkbox label="卖家服务态度恶劣" value="卖家服务态度恶劣" />
          </p>
          <p>
            <el-checkbox
              label="虚假库存，长时间末开启服务"
              value="虚假库存，长时间末开启服务"
            />
            <el-checkbox
              label="与卖家产生争议，无法协调"
              value="与卖家产生争议，无法协调"
            />
          </p>
          <p>
            <el-checkbox label="服务质量与描述不符" value="服务质量与描述不符" />
            <el-checkbox label="卖家交付质量太差" value="卖家交付质量太差" />
          </p>
          <p><el-checkbox label="其他" value="其他" /></p>
          <p class="textarea">
            <textarea
              @input="otherReasonChange"
              placeholder="请输入其他终止原因"
              v-model="otherReason"
            ></textarea>
            <span>{{ otherReasonLength }}/{{ otherReasonMaxLength }}</span>
          </p>
        </el-checkbox-group>
      </div>

      <div class="footer">
        <span class="btn" @click="close">我再想想</span>
        <el-button class="btn" @click="cancelOrderHandle" :loading="loading"
          >取消订单</el-button
        >
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.dialog {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: rgba(25, 30, 40, 0.4);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-box {
  width: 520px;
  background: #ffffff;
  border-radius: 12px 12px 12px 12px;
  padding: 24px;

  .header {
    display: flex;
    align-items: center;
    padding-bottom: 20px;

    .left {
      flex: 1;
      display: flex;
      align-items: center;

      img {
        width: 20px;
        height: 20px;
        margin-right: 4px;
      }

      span {
        font-size: 16px;
        color: #191e28;
        font-weight: bold;
      }
    }

    .close {
      width: 18px;
      height: 18px;
      cursor: pointer;
    }
  }
  .footer {
    display: flex;
    padding: 32px 0 0;
    justify-content: flex-end;
    span {
      width: 92px;
      height: 36px;
      cursor: pointer;
    }
    .btn:nth-child(1) {
      background: #ffffff;
      border-radius: 8px 8px 8px 8px;
      border: 1px solid rgba(25, 30, 40, 0.08);
      display: flex;
      align-items: center;
      justify-content: center;
      color: rgba(25, 30, 40, 0.6);
      margin-right: 12px;
    }
    .btn:nth-child(2) {
      background: #1dbf73;
      border-radius: 8px 8px 8px 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
      height: 94px;
      height: 38px;
      border: none;
    }
  }
  .dialog-content p {
    display: flex;
  }
  .textarea {
    margin-top: 10px;
    position: relative;
    textarea {
      width: 100%;
      height: 96px;
      background: rgba(25, 30, 40, 0.02);
      border-radius: 12px 12px 12px 12px;
      border: 1px solid rgba(25, 30, 40, 0.08);
      padding: 16px;
      box-sizing: border-box;
      resize: none;
    }
    span {
      position: absolute;
      right: 10px;
      bottom: 14px;
      font-size: 12px;
      color: rgba(25, 30, 40, 0.3);
    }
  }
}
</style>
<style>
.rate .el-rate .el-rate__icon {
  font-size: 24px;
  margin-right: 0;
}
.dialog-content p label {
  flex: 1;
}
.dialog-content p .el-checkbox__input .el-checkbox__inner {
  width: 20px;
  height: 20px;
  border-radius: 4px;
}
.dialog-content p .el-checkbox__label {
  font-weight: bold;
  color: #191e28;
}
.dialog-content .el-checkbox__inner:after {
  top: 4px;
  left: 7px;
}
.dialog-content .el-checkbox__input.is-checked + .el-checkbox__label {
  color: #1dbf73 !important;
}
.dialog-content .el-checkbox__input.is-checked .el-checkbox__inner {
  background: #1dbf73;
  border-color: #1dbf73;
}
</style>
